package com.mz.ozy.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.rollviewpager.RollPagerView;
import com.mz.ozy.R;
import com.mz.ozy.adapter.ReciproBannerAdapter;
import com.mz.ozy.adapter.ReciprocityAdapter;
import com.mz.ozy.adapter.ScheduledBannerAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.ReciprocityBean;
import com.mz.ozy.mvp.presenter.ReciprocityPresenter;
import com.mz.ozy.mvp.view.ReciprocityContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReciprocityActivity extends BaseActivity<ReciprocityContract.View, ReciprocityPresenter> implements ReciprocityContract.View {

    @BindView(R.id.srl_refresh)
    SmartRefreshLayout mSmartRefreshLayout;
    @BindView(R.id.rv_goods_list)
    RecyclerView mRvGoodsList;


    @BindView(R.id.rollPagerView)
    RollPagerView rollPagerView;
    @BindView(R.id.iv_bannerPager)
    ImageView ivBannerPager;


    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private ReciproBannerAdapter mBannerAdapter;
    private ReciprocityAdapter mReciprocityAdapter;
    private List<ReciprocityBean.GoodsPageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private List<ReciprocityBean.ImgBannerBean> mBannerListBeans = new ArrayList<>();
    private int tag;
    private int pageNo = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reciprocity);
        ButterKnife.bind(this);
        initTitle();
        final String title = getIntent().getStringExtra("title");
        tvCenterTitle.setText(title);
        tvTitle.setVisibility(View.GONE);

        initView();
        initRefreshLayout();
        mPresenter.queryExpertDetail(this, pageNo);
    }

    private void initView() {
        mBannerAdapter = new ReciproBannerAdapter(this, mBannerListBeans);
        rollPagerView.setAdapter(mBannerAdapter);
        mRvGoodsList.setLayoutManager(new GridLayoutManager(this, 2));
        mRvGoodsList.setNestedScrollingEnabled(false);
        mReciprocityAdapter = new ReciprocityAdapter(mBeanList);
        mRvGoodsList.setAdapter(mReciprocityAdapter);
        mReciprocityAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                final ReciprocityBean.GoodsPageBean.PageItemsBean productListBean = mReciprocityAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.ll_item_view: {
                        Intent intent = new Intent(ReciprocityActivity.this, ExchangeDetailActivity.class);
                        intent.putExtra("goodsId", productListBean.getId() + "");
                        intent.putExtra("gid", productListBean.getGid() + "");
                        intent.putExtra("name", productListBean.getName());
                        startActivity(intent);
                        break;
                    }
                }
            }
        });
    }

    private void initRefreshLayout() {
        mSmartRefreshLayout.setEnableAutoLoadmore(true);
        mSmartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.queryExpertDetail(ReciprocityActivity.this, pageNo);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.queryExpertDetail(ReciprocityActivity.this, pageNo);
            }
        });
    }

    @Override
    public void getExpertDetail(ReciprocityBean bean) {
        if (tag == 0) {
            mBannerListBeans.clear();
            final List<ReciprocityBean.ImgBannerBean> bannerList = bean.getImglist();
            if (bannerList != null && bannerList.size() > 0) {
                for (ReciprocityBean.ImgBannerBean bannerListBean : bannerList) {
                    mBannerListBeans.add(bannerListBean);
                }
            } else {
                rollPagerView.setVisibility(View.GONE);
                ivBannerPager.setVisibility(View.VISIBLE);
            }
            mBannerAdapter.notifyDataSetChanged();
            if (mBannerListBeans.size() == 1) {
                rollPagerView.setHintView(null);
            }
            mSmartRefreshLayout.setLoadmoreFinished(false);
            mSmartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            mSmartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getPage().getPageItems());
        mReciprocityAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            mSmartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }
}
